package com.hisense.ovcloud.mqtt.business.config;

//@Component
//@Slf4j
//@Order
public class MqttListener {
//
//    private final MqttMsgHandler mqttMsgHandler;
//
//    public final MqttClientConfig mqttClientConfig;
//
//    public MqttListener(MqttClientConfig mqttClientConfig, MqttMsgHandler mqttMsgHandler) {
//        this.mqttClientConfig = mqttClientConfig;
//        this.mqttMsgHandler = mqttMsgHandler;
//    }
//
//    @Bean
//    @Qualifier("edwinMqttClient")
//    public MqttClient initMqtt() {
//        Vertx vertx = Vertx.vertx();
//        MqttClientOptions options = new MqttClientOptions()
//                .setMaxMessageSize(100_000_000)
//                .setCleanSession(false)
//                .setAutoKeepAlive(true)
//                .setKeepAliveInterval(2)
//                .setClientId("0/0/999999999999999_1_1_" + System.currentTimeMillis() / 1000)
//                .setUsername("0/0/999999999999999")
//                .setPassword("0/0/999999999999999");
//        options.setReconnectAttempts(Integer.MAX_VALUE);
//        options.setReconnectInterval(3000L);
//
//        MqttClient client = MqttClient.create(vertx, options);
//
//        client.connect(mqttClientConfig.getPlainTextPort(), mqttClientConfig.getBrokerHost(), ch -> {
//            if (ch.succeeded()) {
//                log.info("Connected to emqx: {}", mqttClientConfig.getBrokerHost() +":"+ mqttClientConfig.getPlainTextPort());
//                client.subscribe(Constants.MQTT.Topics.UPDATE_CON, MqttQoS.EXACTLY_ONCE.value());
//                client.subscribe(Constants.MQTT.Topics.UPDATE_GET_DKEY, MqttQoS.EXACTLY_ONCE.value());
//                client.subscribe(Constants.MQTT.Topics.SET_R, MqttQoS.EXACTLY_ONCE.value());
//            } else {
//                log.error("Failed to connect to emqx: {}, reason: {}", mqttClientConfig.getBrokerHost() +":"+ mqttClientConfig.getPlainTextPort(), ch.cause().toString());
//            }
//        });
//
//        client.subscribeCompletionHandler(h -> {
//            log.info("Subscribe complete, levels" + h.grantedQoSLevels());
//        });
//
//        client.publishHandler(s -> {
//            mqttMsgHandler.handleOnMessageReceive(s, client);
//        });
//
//        return client;
//    }
}
